package com.yx.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.dto.OrderDto;
import com.yx.system.dto.TbOrderBookDto;
import com.yx.system.dto.TbOrderDto;
import com.yx.system.entity.*;
import com.yx.system.service.TbKwordGuaranteeService;
import com.yx.system.service.TdBookService;
import com.yx.system.service.TdOrderService;
import com.yx.system.service.TdSectionService;
import com.yx.utils.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/** 后台管理订单controller
 * Created by wuye on 2017/12/26.
 */
@RestController
@RequestMapping("/sys/order")
public class TdOrderController2 {

    @Autowired
    private TdOrderService tdOrderService;

    @Autowired
    private TdBookService tdBookService;

    @Autowired
    private TdSectionService tdSectionService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbKwordGuaranteeService tbKwordGuaranteeService;

    /**
     * 查询所有订单列表
     * @return
     */
    @GetMapping("findAll")
    @RequiresPermissions("sys:order:list")
    public R findAll(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        query.put("orderstatus","success");
        Page<TdOrder> pageUtil = new Page<TdOrder>(query.getPage(), query.getLimit());
        Page<TdOrder> page = tdOrderService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    @RequestMapping("/findById/{id}")
    @RequiresPermissions("sys:order:findById")
    public R findById(@PathVariable("id") int id) throws Exception {
        TdOrder tdOrder = tdOrderService.selectByPrimaryKey(id);
        TdBook tdBook = tdBookService.selectByPrimaryKey(tdOrder.getBookid());
        TdSection tdSection = tdSectionService.selectByPrimaryKey(tdOrder.getSectionid());
        OrderDto dto = new OrderDto();
        dto.setTdBook(tdBook);
        dto.setTdSection(tdSection);
        dto.setTdOrder(tdOrder);
        return R.ok().put("dto",dto);
    }

    /*
    * 获取作者订阅图书列表
    */
    @RequestMapping("orderList")
    public R getOrderList(@LoginUser2 SysUser sysUser, @RequestParam Map<String, Object> params){
        long a = System.currentTimeMillis();
        if (sysUser.getIdentity()==0){
            return R.error("作者才有权查看图书评论");
        }
        //查询对应的用户
        TbUser tbUser = tbUserService.findByMobile(sysUser.getUsername());
        if (tbUser.getIdentity()==0){
            return R.error("用户的身份不是作者");
        }
        //查询列表数据
        Query query = new Query(params);
        query.put("authorCode",tbUser.getUuid());
        List<TdBook> tdBooks = tdBookService.queryList(query);
        int total = tdBookService.queryTotal(query);
        //初始化数据传输对象集合
        List<TbOrderBookDto> tbOrderBookDtos = new ArrayList<>();
        //取出保底收入对象
        List<TbKwordGuarantee> tbKwordGuarantees = tbKwordGuaranteeService.findAll();
        if (tbKwordGuarantees.size()!=1){
            return R.error("出现了预期之外的错误，请联系管理员");
        }
        TbKwordGuarantee tbKwordGuarantee = tbKwordGuarantees.get(0);
        //循环图书集合
        for (TdBook tdBook:tdBooks){
            //计算实际千字数
            Double font = Double.valueOf(tdBook.getFontCount()/1000);
            //计算保底收入
            tdBook.setGuarantee(font*tbKwordGuarantee.getGuarantee());
            tdBookService.updateByPrimaryKey(tdBook);
            //初始化数据传输对象
            TbOrderBookDto tbOrderBookDto = new TbOrderBookDto();
            //初始化一个查询参数
            Date date = new Date();
            Map<String,Object> map = new HashMap<>();
            map.put("bookid",tdBook.getId());
            map.put("year", DateUtil.getYear(date));
            map.put("month",DateUtil.getMonth(date));
            //初始化统计的值
            Integer guaranteeMonth = 0;
            Integer guaranteeDay = 0;
            //计算月统计
            guaranteeMonth = tdOrderService.findByCondition(map);
            map.put("day",DateUtil.getDay(date));
            //计算日统计
            guaranteeDay = tdOrderService.findByCondition(map);
            //将属性注入DTO
            tbOrderBookDto.setGuaranteeDay(guaranteeDay);
            tbOrderBookDto.setGuaranteeMonth(guaranteeMonth);
            tbOrderBookDto.setTdBook(tdBook);
            tbOrderBookDtos.add(tbOrderBookDto);
        }
        PageUtils pageUtil = new PageUtils(tbOrderBookDtos, total, query.getLimit(), query.getPage());
        long b = System.currentTimeMillis();
        System.out.println("======================="+(b-a)+"============================");
        return R.ok().put("page",pageUtil);
    }

    /*
    * 根据作者图书查看这本书的订单
    */
    @RequestMapping("getOrder")
    @RequiresPermissions("sys:order:findById")
    public R findByBook(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<TdOrder> tdOrders = tdOrderService.queryList(query);
        //初始化dto传输对象
        List<TbOrderDto> orderDtos = new ArrayList<>();
        //循环查询书名和章节名
        for (TdOrder tdOrder:tdOrders){
            //初始化DTO
            TbOrderDto tbOrderDto = new TbOrderDto();
            //查询图书
            TdBook tdBook = tdBookService.selectByPrimaryKey(tdOrder.getBookid());
            //查询章节
            TdSection tdSection = tdSectionService.selectByPrimaryKey(tdOrder.getSectionid());
            tbOrderDto.setTdOrder(tdOrder);
            tbOrderDto.setBookname(tdBook.getBookname());
            tbOrderDto.setSectionname(tdSection.getSectionname());
            orderDtos.add(tbOrderDto);
        }
        int total = tdOrderService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(orderDtos, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }
}
